package com.joshua.spring.controller;

import com.joshua.spring.entity.Provider;
import com.joshua.spring.entity.dto.ProviderQueryCondition;
import com.joshua.spring.entity.form.store.DelProviderForm;
import com.joshua.spring.entity.form.store.ProviderForm;
import com.joshua.spring.entity.vo.GlobalResponse;
import com.joshua.spring.service.ProviderInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.joshua.spring.constant.ProviderApiConstants.*;
import static com.joshua.spring.constant.ProviderApiConstants.StoreInfo.STORE_INFO;
import static com.joshua.spring.constants.SymbolConstants.COLON;

/**
 * projectName feign-test
 * package com.joshua.spring.controller
 * classname  ProviderStoreController
 * <p>
 * description
 * </p>
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/10/20 16:07
 */
@Api(tags = "生产者持久化")
@RestController
@RequestMapping(STORE_INFO)
public class ProviderStoreController {
    @Autowired
    private ProviderInfoService providerInfoService;

    @GetMapping(QUERY_LIST)
    public GlobalResponse listStoreInfo(@ModelAttribute ProviderForm queryConditionForm) {
        ProviderQueryCondition condition = new ProviderQueryCondition();
        BeanUtils.copyProperties(queryConditionForm, condition);
        return GlobalResponse.buildSuccess(providerInfoService.query(condition));
    }

    @PutMapping(PATH_ID)
    @Transactional(rollbackFor = Exception.class)
    public GlobalResponse updateStoreProviderInfo(@PathVariable(name = ID) Integer providerId,
                                                  @RequestBody ProviderForm form) {
        Provider provider = new Provider();
        BeanUtils.copyProperties(form, provider);
        provider.setProviderId(providerId);
        Integer result = providerInfoService.updateProviderInfo(provider);
        return GlobalResponse.buildSuccess(result);
    }

    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public GlobalResponse insertProviderInfo(@RequestBody ProviderForm form) {
        Provider provider = new Provider();
        BeanUtils.copyProperties(form, provider);
        Integer result = providerInfoService.insertProviderInfo(provider);
        return GlobalResponse.buildSuccess(result);
    }

    @DeleteMapping(PATH_ID + COLON)
    @Transactional(rollbackFor = Exception.class)
    public GlobalResponse delProviderInfo(@PathVariable(name = ID, required = false) Integer providerId,
                                          @RequestBody DelProviderForm form) {
        List<Integer> ids = new ArrayList<>();
        if (!ObjectUtils.isEmpty(form) && !CollectionUtils.isEmpty(form.getProviderIds())) {
            ids = form.getProviderIds();
        }
        if (!ObjectUtils.isEmpty(providerId)) {
            ids.add(providerId);
        }
        if (CollectionUtils.isEmpty(ids)) {
            return GlobalResponse.buildSuccess();
        }
        Integer result = providerInfoService.deleteProviderInfos(ids);
        return GlobalResponse.buildSuccess(result);
    }

}
